﻿using System;
using System.Collections;
using System.Runtime.InteropServices;
using UnityEngine;

public class TransparentWindow : MonoBehaviour
{
    [DllImport("user32.dll")]
    private static extern IntPtr GetActiveWindow();
    
    [DllImport("user32.dll")]
    private static extern int SetWindowLong(IntPtr hWnd, int nIndex, uint dwNewLong);
    
    [DllImport("user32.dll")]
    private static extern bool SetWindowPos(IntPtr hWnd, IntPtr hWndInsertAfter, int X, int Y, int cx, int cy, uint uFlags);
    
    [DllImport("user32.dll")]
    private static extern bool SetLayeredWindowAttributes(IntPtr hwnd, uint crKey, byte bAlpha, uint dwFlags);
    
    
    private const int GWL_EXSTYLE = -20;
    private const int GWL_STYLE = -16;
    private const uint WS_EX_LAYERED = 0x00080000;
    private const uint WS_EX_TOPMOST = 0x00000008;
    private const uint WS_POPUP = 0x80000000;
    private const uint WS_VISIBLE = 0x10000000;
    private const uint WS_EX_TOOLWINDOW = 0x00000080;   // 可以用来隐藏
    private static readonly IntPtr HWND_TOPMOST = new IntPtr(-1);
    private const uint SWP_SHOWWINDOW = 0x0040;
    private const uint LWA_COLORKEY = 0x00000001;
    private IntPtr windowHandle;
    private const uint SWP_NOACTIVATE = 0x0010;
    IEnumerator Start()
    {
        
        yield return new WaitForSeconds(1f); // 等待 Unity 窗口创建完成
        MakeTransparent();
    }
    
    void MakeTransparent()
    {
        windowHandle = GetActiveWindow();
        // 移除窗口边框
        SetWindowLong(windowHandle, GWL_STYLE, WS_POPUP | WS_VISIBLE);
        
        // 设置为分层窗口和置顶
        SetWindowLong(windowHandle, GWL_EXSTYLE, WS_EX_LAYERED | WS_EX_TOPMOST );
        
        // 设置全屏
        SetWindowPos(windowHandle, HWND_TOPMOST, 0, 0, Screen.currentResolution.width, Screen.currentResolution.height, SWP_SHOWWINDOW);
        
        // 设置黑色为透明色键
        SetLayeredWindowAttributes(windowHandle, 0x006B6B6B, 255, LWA_COLORKEY);
    }
    
}